package com.imooc.web;

import com.imooc.bo.AddressBo;
import com.imooc.pojo.UserAddress;
import com.imooc.service.AddressService;
import com.imooc.utils.MobileEmailUtils;
import com.imooc.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2022-02-20 15:56
 */
@RestController
@Api(value = "地址相关api", tags = {"地址相关的api接口"})
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 1. 查询所有用户收货地址
     * 2. 修改收货地址
     * 3. 新增收货地址
     * 4. 设置默认收货地址
     * 5. 删除收货地址
     */

    @ApiOperation(value = "根据用户id查询收货地址", notes = "根据用户id查询收货地址" , httpMethod = "POST")
    @PostMapping("/list")
    public Result list(
            @ApiParam(name = "userId",value = "用户编号",required = true) @RequestParam   String userId
    ) {
        if(StringUtils.isBlank(userId)) {
            return Result.errorMsg("用户编号不得为空");
        }
        List<UserAddress> userAddresses = addressService.queryAll(userId);

        return Result.ok(userAddresses);
    }

    @ApiOperation(value = "添加收货地址", notes = "添加收货地址" , httpMethod = "POST")
    @PostMapping("/add")
    public Result add(
            @RequestBody AddressBo addressBo, BindingResult bindingResult
            ) {
        if(bindingResult.hasErrors()) {
            String collect = bindingResult.getAllErrors().stream().map(e -> e.getObjectName() + ":" + e.getDefaultMessage()).collect(Collectors.joining());
            return Result.errorMsg(collect);
        }
        if(!MobileEmailUtils.checkMobileIsOk(addressBo.getMobile())) {
            return Result.errorMsg("手机号码格式不正确");
        }
        addressService.addNewUserAddress(addressBo);

        return Result.ok();
    }

    @ApiOperation(value = "修改收货地址", notes = "修改收货地址" , httpMethod = "POST")
    @PostMapping("/update")
    public Result update(
            @RequestBody AddressBo addressBo, BindingResult bindingResult
    ) {
        if(bindingResult.hasErrors()) {
            String collect = bindingResult.getAllErrors().stream().map(e -> e.getObjectName() + ":" + e.getDefaultMessage()).collect(Collectors.joining());
            return Result.errorMsg(collect);
        }
        if(StringUtils.isBlank(addressBo.getUserId())) {
            return Result.errorMsg("用户编号不得为空");
        }
        if(!MobileEmailUtils.checkMobileIsOk(addressBo.getMobile())) {
            return Result.errorMsg("手机号码格式不正确");
        }
        addressService.updateUserAddress(addressBo);

        return Result.ok();
    }
    @ApiOperation(value = "删除收货地址", notes = "删除收货地址" , httpMethod = "POST")
    @PostMapping("/delete")
    public Result delete(
        @ApiParam(name = "addressId",value = "地址编号", required = true) @RequestParam String addressId,
        @ApiParam(name = "userId",value = "用户编号", required = true) @RequestParam String userId
    ) {
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)) {
            return Result.errorMsg("");
        }
       int i =  addressService.deleteUserAddress(addressId,userId);

        return i>0?Result.ok():Result.errorMsg("地址不存在");
    }

    @PostMapping("/setDefault")
    public Result setDefault(
            @ApiParam(name = "addressId",value = "地址编号", required = true) @RequestParam String addressId,
            @ApiParam(name = "userId",value = "用户编号", required = true) @RequestParam String userId
    ) {
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)) {
            return Result.errorMsg("");
        }
        int i =  addressService.setDefaultAddress(addressId,userId);

        return i>0?Result.ok():Result.errorMsg("地址不存在");
    }
}
